/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2010
Version 0.1
=======================================================*/
#include "data/User.h"

namespace acs
{

User::User() : type(ACS_USER_GUEST)
{

}

User::~User()
{

}

void User::setID(const int id)
{
	this->id = id;
}

int User::getID() const
{
	return this->id;
}

void User::setType(const UserType type)
{
	this->type = type;
}

UserType User::getType() const
{
	return this->type;
}

std::string User::getTypeStr() const
{
	switch(this->type)
	{
		case ACS_USER_GUEST:
		{
			return "guest";
		}break;

		case ACS_USER_NORMAL:
		{
			return "normal";
		}break;

		case ACS_USER_ADMIN:
		{
			return "with commands";
		}break;

		default:
		{
			return "guest";
		}break;
	}
}

void User::setUpdated(time_t updated)
{
	this->updated = updated;
}

time_t User::getUpdated() const
{
	return this->updated;
}

void User::setCreation(time_t creation)
{
	this->creation = creation;
}

time_t User::getCreation() const
{
	return this->creation;
}

void User::setName(const std::string& name)
{
	this->name = name;
}

std::string User::getName() const
{
	return this->name;
}

void User::setLogin(const std::string& login)
{
	this->login = login;
}

std::string User::getLogin() const
{
	return this->login;
}

void User::setPassword(const std::string& password, bool encrypted)
{
	if(encrypted)
		this->password = password;
	else
		this->password = this->encryptPassword(password);
}

std::string User::getPassword() const
{
	return this->password;
}

bool User::validatePassword(const std::string& password)
{
	return (this->password.compare(this->encryptPassword(password)) == 0);
}

std::string User::encryptPassword(const std::string& password)
{
	char digest[SHA_DIGEST_SIZE];

	rgcpp::SHAGenerator::digest(password.c_str(),password.size(),digest);

	return rgcpp::ToHex(digest,SHA_DIGEST_SIZE);
}

}
